﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//形状接口
public interface IShape
{
    double CalculateArea();
    bool IsValid();
}

//抽象类进行接口实现：
public abstract class Shape : IShape
{
    public abstract double CalculateArea();
    public abstract bool IsValid();
}

//长方形类：
public class Rectangle : Shape
{//长、宽
    public double Width { get; set; }
    public double Height { get; set; }
    //构造函数
    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }
    //计算面积
    public override double CalculateArea()
    {
        return Width * Height;
    }
    //查看是否有效
    public override bool IsValid()
    {
        return Width > 0 && Height > 0;
    }
}

//正方形：
public class Square : Shape
{ //边长：
    public double Side { get; set; }
    //构造函数：
    public Square(double side)
    {
        Side = side;
    }
    //面积：
    public override double CalculateArea()
    {
        return Side * Side;
    }
    //有效：
    public override bool IsValid()
    {
        return Side > 0;
    }
}

//三角形：
public class Triangle : Shape
{ //三条边：
    public double SideA { get; set; }
    public double SideB { get; set; }
    public double SideC { get; set; }
    //构造函数：
    public Triangle(double sideA, double sideB, double sideC)
    {
        SideA = sideA;
        SideB = sideB;
        SideC = sideC;
    }
    //计算面积：
    public override double CalculateArea()
    {
        double m = (SideA + SideB + SideC) / 2;
        return Math.Sqrt(m * (m - SideA) * (m - SideB) * (m - SideC));
    }
    //判断是否有效：
    public override bool IsValid()
    {
        return ((SideA + SideB > SideC) && (SideA + SideC > SideB) && (SideB + SideC > SideA));
    }
}
//简单工厂：
public static class ShapeFactory
{
    private static Random random = new Random();

    //创建随机形状
    public static IShape CreateRandomShape()
    {
        int shapeType = random.Next(0, 3);
        //0,1,2分别对应长方形、正方形、三角形
        switch (shapeType)
        {
            case 0:
                double width = random.Next(1, 10);
                double Height= random.Next(1, 10);
                return new Rectangle(width, Height);
            case 1:
                double Side = random.Next(1, 10);
                return new Square(Side);
            case 2:
                double a = random.Next(1, 10);
                double b = random.Next(1, 10);
                double c = random.Next(1, 10);
                return new Triangle(a,b,c);
            default:
                throw new InvalidOperationException("类型不正确！");
        }
    }
}
namespace TenShape
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<IShape> shapes = new List<IShape>();
            for (int i = 0; i < 10; i++)
            {//随机创造出来的形状可能并不符合形状特征，即invalue,把这些东西删掉，直到产生10个value的形状并计算它们的面积之和
                IShape shape = ShapeFactory.CreateRandomShape();
                if (shape.IsValid())
                {
                    shapes.Add(shape);
                    Console.WriteLine($"创造了一个{shape.GetType().Name},面积是：{shape.CalculateArea()}");
                }
                else i--;
            }
            //面积之和：
            double totalArea = 0;
            foreach (var shape in shapes)
            {
                totalArea += shape.CalculateArea();
            }
            Console.WriteLine($"总面积是：{totalArea}");

            Console.ReadLine();
        }
     
    }
}
